package com.iisigroup.http.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import org.apache.commons.io.IOUtils;

public class ReadSampleFile {
	private static final  String srcPath = "/home/weblogic/Desktop/rcdf002d";

	public static void main(String[] args)     {
		testMethod02();
		
	}
	public static void testMethod01(){
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(srcPath));
			while(br.ready()){
				String line = br.readLine();
				System.out.println(line.length());
				;
			}
		} catch(Exception e){
			
		}finally{
			
			if(br != null ){
				try {
					br.close();
				} catch (IOException e) {
				}
			}
		}
	}
	public static void testMethod02(){

		 String line = getRandomline();
		 System.out.println(line );
	
	}
	public static String getRandomline(){
		String result =null;
		RandomAccessFile raf = null;
		try {
			// create a new RandomAccessFile with filename test
			raf = new RandomAccessFile(new File(srcPath), "r");
			int lines =(int) raf.length()/28;		
			int randomInt = ThreadLocalRandom.current().nextInt(lines);			
			raf.seek(randomInt * 28);
			String pId = raf.readLine();
			if (pId.length() == 27) {
				result = pId;
			}else{
				randomInt = ThreadLocalRandom.current().nextInt(lines);			
				raf.seek(randomInt * 28);
				result =  raf.readLine();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}finally{
			if (raf != null) {
				try {
					raf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result ;
	}
}
